package com.cd468.game;

import java.util.ArrayList;
import java.util.List;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;

import com.cd468.share.MovieView;
import com.cd468.share.Tool;

public class Bullet extends MovieView {

	public Bullet(Context context, Rect ViewRect) {
		super(context, ViewRect);
	}
	
	static List<MovieView> sItemList = new ArrayList<MovieView>();
	float mRotate=0;
	static void New(int x,int y)
	{
		
		float rotate = PaoTai.GetRotate(x, y);
		if(Math.abs(rotate)>80)
		{
			return;
		}
		PaoTai.Change(x, y);
		Bullet obj = new Bullet(Data.sContext,Tool.CloneRect((Data.sGameWidth-20)/2,Data.sGameHeight-40-100,20,40));
		obj.mRotate = rotate;
		obj.ShowView();
		Score.Add(-5);
	}
	void SetRotate()
	{ 
        this.setRotation(mRotate);
        this.setPivotX(Data.sGameWidth/2);
        this.setPivotY(Data.sGameHeight);
	}
	void setBulletPos(int num)
	{
		int top =  num;
		int width = mViewRect.right - mViewRect.left;
		int height = mViewRect.bottom - mViewRect.top;
		mViewRect = Tool.CloneRect(mViewRect.left, top, width, height);
		SetRotate();
		this.invalidate();
	}
	void ShowView()
	{
		
		this.AddView(R.drawable.bullet3);		
		Data.addView(this);
		this.Show();        
		SetRotate();
        
		sItemList.add(this);
		
		int x; 
        int y; 
        double radians = Math.toRadians(mRotate);
        double angel = (double)Data.sGameHeight*2/Data.sGameWidth;
        double reallyangel = Math.tan(radians);
        System.out.println(angel+"====="+reallyangel);
        if(mRotate > 0){

            if (Math.abs(reallyangel)<angel){
                x = (int) (Data.sGameHeight*Math.tan(radians));
                y = -Data.sGameHeight;
            } else {
                x = Data.sGameWidth/2;
                y = -(int) (Data.sGameHeight/Math.tan(radians));
            }
        } else {
            if (Math.abs(reallyangel)<angel){
                x = (int) (Data.sGameHeight*Math.tan(radians));
                y = -Data.sGameHeight;
            } else {
                x = -Data.sGameWidth/2;
                y = (int) (Data.sGameHeight/Math.tan(radians));
            }
        }

    	
        final ObjectAnimator animatorx = ObjectAnimator.ofFloat(this, "translationX", 0, x);
        animatorx.setDuration(3000);
        animatorx.start();
        final ObjectAnimator animatory = ObjectAnimator.ofFloat(this, "translationY", 0,y);
        animatory.setDuration(3000);
        animatory.start();


        animatory.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
            	
            	Bullet obj = Bullet.this;
            	obj.ClearView();
            	sItemList.remove(obj);
            	animatorx.cancel();
            	animatory.cancel();
            	Data.removeView(obj);
            	
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        animatorx.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
            	Bullet obj = Bullet.this;
            	if(Fighter.checkBang(obj))
            	{
            		obj.ClearView();
                	sItemList.remove(obj);
                	animatorx.cancel();
                	animatory.cancel();
                	Data.removeView(obj);
            	}
            }
        });		
        
	}

}
